﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.Language;
using WebApiV001.Domain.Entity;
using WebApiV001.Interface;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RolesController : ControllerBase
    {
        private readonly IRepository<Roles> _rolesRespository;
        private readonly IRepository<RolesPermission> _rolesPermissionRespository;

        public RolesController(IRepository<Roles> rolesRespository, IRepository<RolesPermission> rolesPermissionRespository)
        {
            _rolesRespository = rolesRespository;
            _rolesPermissionRespository = rolesPermissionRespository;
        }

        // GET: api/<RolesController>
        [HttpGet]
        public ActionResult Get()
        {
            var list = _rolesRespository.Table
                .Where(x => x.IsActived == true && x.IsDeleted == false)
                .ToList();

            return Ok(new { code = 1000, data = list, msg = "获取角色列表成功" });
        }

        // GET api/<RolesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<RolesController>
        [HttpPost]
        public ActionResult Post([FromBody] Roles roles)
        {
            var arr = new Roles
            {
                RoleName = roles.RoleName,
                Descripiton = roles.Descripiton
            };

            _rolesRespository.Add(arr);

            return Ok(new { code = 1000, data = arr, msg = "添加角色成功" });
        }

        // PUT api/<RolesController>/5
        [HttpPut("{id}")]
        public ActionResult Put(int id, [FromBody] Roles roles)
        {
            var role = _rolesRespository.GetById(id);

            if (role != null)
            {
                role.RoleName = roles.RoleName;
                role.Descripiton = roles.Descripiton;

                _rolesRespository.Update(role);
                return Ok(new { code = 1000, data = role, msg = "修改成功" });

            }
            else
            {
                return Ok(new { code = 1000, data = "", msg = "修改失败" });
            }



        }

        // DELETE api/<RolesController>/5
        [HttpDelete("{id}")]
        public ActionResult Delete(int id)
        {
            var role = _rolesRespository.GetById(id);

            if (role != null)
            {
                role.IsActived = false;
                role.IsDeleted = true;

                _rolesRespository.Update(role);

                return Ok(new { code = 1000, data = "", msg = "删除成功" });
            }
            else
            {
                return Ok(new { code = 1000, data = "", msg = "删除失败" });
            }

        }

        [HttpPost, Route("saveRolepermission")]
        public ActionResult SaveRolepermission([FromBody] RolespermissionDto rolepermission)
        {
            // 获取角色已有权限列表
            var role = _rolesPermissionRespository.Table.Where(x => x.RolesId == rolepermission.RoleId);

            if (rolepermission.Access != null)
            {
                if (role.Count() > 0)
                {
                    // 删除角色已有权限列表
                    _rolesPermissionRespository.Delete(role);

                }
                // 添加角色权限
                var accessList = new List<RolesPermission>();
                foreach (var item in rolepermission.Access)
                {
                    var acc = new RolesPermission
                    {
                        RolesId = rolepermission.RoleId,
                        PermissionName = item.Name
                    };

                    accessList.Add(acc);
                }

                _rolesPermissionRespository.Add(accessList);

                return Ok(new { code = 1000, data = accessList, msg = "添加成功" });
            }
            else
            {
                var resList = new List<dynamic>();
                foreach(var item in role){
                    resList.Add(item.PermissionName);
                }
                
                return Ok(new { code = 1000, data = resList, msg = "获取权限名称数组成功" });
            }
            
        }

    }
}
